package cc.mrbird.web.controller;

import cc.mrbird.common.annotation.Log;
import cc.mrbird.common.controller.BaseController;
import cc.mrbird.common.domain.QueryRequest;
import cc.mrbird.common.domain.ResponseBo;
import cc.mrbird.system.domain.OneStudent;
import cc.mrbird.system.domain.VisitStudent;
import cc.mrbird.web.service.OneStudentService;
import cc.mrbird.web.service.VisitStudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class VisitStudentController extends BaseController {

	private static final Logger log = LoggerFactory.getLogger(VisitStudentController.class);

	@Autowired
	private VisitStudentService visitStudentService;

	@Value("${sys.admin}")
	private String admin;

	@Log("获取信息")
	@RequestMapping("visit_student")
	@RequiresPermissions("visit_student:list")
	public String index() {
		return "web/visit_student/visit_student";
	}

	@RequestMapping("visitStudent/list")
	@ResponseBody
	public Map<String, Object> visitStudentList(QueryRequest request, VisitStudent visitStudent, String sonUserName) {
		List<VisitStudent> list;
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		if(visitStudent.getUsername().equalsIgnoreCase(admin) && sonUserName == null){
			visitStudent.setUsername("");
			list = this.visitStudentService.findVisitStudentByCondition(visitStudent);
		}else if(visitStudent.getUsername().equalsIgnoreCase(admin) && sonUserName != null){
			visitStudent.setUsername(sonUserName);
			list = this.visitStudentService.findVisitStudentByCondition(visitStudent);
		}else{
			list = this.visitStudentService.findVisitStudentByCondition(visitStudent);
		}
		PageInfo<VisitStudent> pageInfo = new PageInfo<>(list);
		return getDataTable(pageInfo);
	}

	@RequestMapping("visitStudent/getVisitStudent")
	@ResponseBody
	public ResponseBo getUser(String id) {
		try {
			VisitStudent visitStudent = this.visitStudentService.findById(id);
			return ResponseBo.ok(visitStudent);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("获取用户信息失败，请联系网站管理员！");
		}
	}

	@Log("新增来校师生信息")
	@RequiresPermissions("visit_student:add")
	@RequestMapping("visitStudent/add")
	@ResponseBody
	public ResponseBo addVisitStudent(VisitStudent visitStudent,String oneUserName) {
		try {
			this.visitStudentService.addVisitStudent(visitStudent,oneUserName);
			return ResponseBo.ok("新增考生成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("新增信息失败，请检查！");
		}
	}

	@Log("修改来校师生信息")
	@RequiresPermissions("visit_student:update")
	@RequestMapping("visitStudent/update")
	@ResponseBody
	public ResponseBo updateOneStudent(VisitStudent visitStudent) {
		try {
			this.visitStudentService.updateVisitStudent(visitStudent);
			return ResponseBo.ok("修改信息成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("修改信息失败，请联系网站管理员！");
		}
	}

	@Log("删除信息")
	@RequiresPermissions("visit_student:delete")
	@RequestMapping("visitStudent/delete")
	@ResponseBody
	public ResponseBo deleteVisitStudents(String ids) {
		try {
			this.visitStudentService.deleteVisitStudents(ids);
			return ResponseBo.ok("删除信息成功！");
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseBo.error("删除信息失败，请联系网站管理员！");
		}
	}

	@RequestMapping("visitStudent/checkStudentId")
	@ResponseBody
	public boolean checkVisitStudentId(String id, String oldId) {
		if (StringUtils.isNotBlank(oldId) && id.equalsIgnoreCase(oldId)) {
			return true;
		}
		VisitStudent result = this.visitStudentService.findById(id);
		if (result != null)
			return false;
		return true;
	}

	@RequestMapping("visitStudent/checkIdNumber")
	@ResponseBody
	public boolean checkVisitIdNumber(String idNumber, String oldidNumber) {
		if (StringUtils.isNotBlank(oldidNumber) && idNumber.equalsIgnoreCase(oldidNumber)) {
			return true;
		}
		VisitStudent result = this.visitStudentService.findByIdNumber(idNumber);
		if (result != null)
			return false;
		return true;
	}

}
